<?php

class DefaultController extends AdminController {
	public function actionIndex() {
		$this->render('index');
	}
    
	public function actionCountryRegions() {
        $country_id = intval(CHttpRequest::getParam('country_id'));
		if (0 < $country_id) {
			$data = Regions::model()->findAll('hidden = 0 AND country_id=:country_id', array(':country_id'=> $country_id));	
		} else {
			$data = Regions::model()->findAll('hidden = 0');
		}
		$data= CHtml::listData($data,'id','name');
		$options = '';
        $add_default = CHttpRequest::getParam('add_default');
		if (1 == $add_default) {
			$options .= CHtml::tag('option', array('value'=>''), AdminModule::t('Все'),true);
		}
	    foreach($data as $value=>$name) {
	        $options .= CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
	    }		
	    echo $options;
	}   


    public function actionModelKeyOptions() {
        $content = '';
        $pkey = CHttpRequest::getParam('pkey');
        if ('' == $pkey) echo 'pkey is not set';
        $key_value = CHttpRequest::getParam($pkey);
        $OptionsModel = CHttpRequest::getParam('model');
        if ('' == $OptionsModel) echo 'model is undefined';
        $option_value_key = CHttpRequest::getParam('ovkey');
        $option_label_key = CHttpRequest::getParam('olkey');
        
        if (0 <= $key_value) {
            $data = $OptionsModel::model()->findAll('hidden = 0 AND '.$pkey.'=:'.$pkey, array(':'.$pkey=> $key_value));	
        } else {
            $data = $OptionsModel::model()->findAll('hidden = 0');
        }
		$content = '';
		if (isset($_REQUEST['dvkey']) && isset($_REQUEST['dlkey'])) {
            $default_value_key = CHttpRequest::getParam('dvkey');
            $default_label_key = CHttpRequest::getParam('dlkey');
			$content .= CHtml::tag('option', array('value'=>$default_value_key), $default_label_key,true);
		}
        if (0 >= count($data)) echo $content;
        
	    foreach($data as $row) {
            if (isset($row[$option_value_key]) && isset($row[$option_label_key])) {
                $content .= CHtml::tag('option', array('value'=>$row[$option_value_key]),CHtml::encode($row[$option_label_key]),true);    
            }
	    }
        echo $content;   
    }
    
    
}